package cn.tedu.tmall.front.mall.dao.repository.impl;

import cn.tedu.tmall.common.pojo.PageData;
import cn.tedu.tmall.front.mall.dao.mapper.OrderMapper;
import cn.tedu.tmall.front.mall.dao.repository.IOrderRepository;
import cn.tedu.tmall.front.mall.pojo.po.OrderPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OrderRepository implements IOrderRepository {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Integer save(OrderPO orderPO) {
        return orderMapper.insert(orderPO);
    }

    @Override
    public PageData<OrderPO> pageOrderListByUserId(Long userId, Integer pageSize, Integer pageNum) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("buyer_id", userId);
        PageHelper.startPage(pageNum, pageSize);
        List list = orderMapper.selectList(queryWrapper);
        PageInfo<OrderPO> pageInfo = new PageInfo<>(list);
        PageData<OrderPO> pageData = new PageData<>();
        BeanUtils.copyProperties(pageInfo, pageData);
        return pageData;

    }
}
